clear all
*cap log close
set more off
set seed 603

// other stuff -------------
local nbin = 16
// -----------------------------------------------------


// --------------------------------------------------------------------------------
// initialize plotting data ----------------
local Q = 2 

* read in polynomial estimates ------
import delimited using "${est}/recid_poly.csv", clear 
keep if poly == `Q'
replace par = trim(upper(par))
keep if par == "Y0"|par == "Y1"
gen z = (par=="Y1")
gen flag = 1 
gen y    = est
keep z y ub lb flag 
tempfile est 
save    `est'

* store binscatter ------------------
use "${data}/out/4-main", clear 
gen Y = cite_ny1 
keep Y Z totfe 
binsreg Y Z, absorb(totfe) binspos(es) nbins(`nbin') savedata(bs) replace 

* setup initial graphing data ----------
use bs, clear 
rename dots_x   z 
rename dots_fit y
qui append using `est'

tempfile data_graph 
save    `data_graph'
rm bs.dta 
// --------------------------------------------------------------------------------


// --------------------------------------------------------------------------------
// superimpose polynomial fits 

foreach Q in 2 4 8 {

	use "${data}/out/4-main", clear
	gen Y = cite_ny1 

	forval q = 1/`Q' {
		gen z_`q' = Z^`q'
	} 

	reghdfe Y z_*, absorb(totfe)
	local b0 = _b[_cons]
	forval q = 1/`Q' {
		local b`q' = _b[z_`q']
	}	

	use `data_graph', clear 
	gen fit_`Q' = `b0'
	forval q = 1/`Q' {
		replace fit_`Q' = fit_`Q' + (`b`q''*z^`q')
	}

	tempfile data_graph 
	save    `data_graph'

}
// --------------------------------------------------------------------------------



// --------------------------------------------------------------------------------
// add local mean fits 
import delimited using "${est}/recid_bins.csv", clear 
keep if type == "mean"
replace par = trim(upper(par))
replace bw  = int(bw*100)
replace bw  = 1 if bw==0 

keep if par == "Y0"|par == "Y1"
keep if bw == 10|bw == 5|bw == 1 

gen temp = bw/100
gen     z = . 
replace z = temp/2 if par=="Y0"
replace z = 1-(temp/2) if par=="Y1"

rename est y_mean
keep z y bw  
tempfile means 
save    `means'

use `data_graph', clear 
merge 1:1 z using `means', nogen 
tempfile data_graph 
save    `data_graph'
// ----------------------------------------------------------------------------------


// --------------------------------------------------------------------------------
// superimpose local linear fit 
use "${data}/out/4-main", clear
gen Y = cite_ny1 

binsreg Y Z, absorb(totfe) binspos(es) nbins(5) line(1 1) savedata(bs) replace 
use bs, clear 
rename line_x   z 
rename line_fit fit_spline 
keep z fit_spline 

tempfile spline 
save    `spline'

use `data_graph', clear 
merge 1:1 z using `spline', nogen 
tempfile data_graph 
save    `data_graph'
// --------------------------------------------------------------------------------



// --------------------------------------------------------------------------------
// construct plot 
#delimit ;
scatter y z, msymbol(O) mcolor(gs12) msize(medsmall) || 
line fit_2 z, lcolor(dkgreen) ||
scatter y z if flag==1, mcolor(dkgreen) msize(med) ||
rspike ub lb z if flag==1, lcolor(dkgreen) ||
line fit_4 z, lcolor(dkorange) lpattern(shortdash) ||
line fit_8 z, lcolor(maroon) lpattern(dash) ||
line fit_spline z, lcolor(purple) lpattern(longdash) ||
scatter y_mean z if bw==10, msymbol(T) msize(medium) mcolor(gold)||
scatter y_mean z if bw==5, msymbol(D) msize(medium) mcolor(cranberry) ||
scatter y_mean z if bw==1, msymbol(S) msize(medium) mcolor(magenta)
graphregion(color(white)) plotregion(lcolor(black) lwidth(medthin)) 
ylab(0.33(0.01)0.38,nogrid) xlab(,nogrid)
xtitle("Officer Stringency") ytitle("Pr(Reoffend)")
legend(ring(0) cols(2) pos(2) size(small) region(lstyle(border))
order(5 8 6 9 7 10) lab(5 "Quartic") lab(6 "8th Order") lab(7 "Piecewise Linear")
lab(8 "Tail Mean: 10%") lab(9 "Tail Mean: 5%") lab(10 "Tail Mean: 1%")) ;
#delimit cr
graph export "${out}/apx_extrap/fform.pdf", replace
// --------------------------------------------------------------------------------


